package com.shark.springbootcms.service.impl;

import com.shark.springbootcms.common.Result;
import com.shark.springbootcms.form.MenuForm;
import com.shark.springbootcms.mapper.MenuMapper;
import com.shark.springbootcms.service.MenuService;
import com.shark.springbootcms.utils.IdWorker;
import com.shark.springbootcms.vo.MenuVo;
import com.shark.springbootcms.vo.OptionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private IdWorker idWorker;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public Result addMenu(MenuForm form) {
        form.setId(String.valueOf(idWorker.nextId()));
        String parentName = null;
        String parentId = null;
        if (!CollectionUtils.isEmpty(form.getParentName())) {
            parentId = form.getParentName().get(form.getParentName().size() - 1);
            parentName = StringUtils.join(form.getParentName(), ",");
        }
        menuMapper.addMenu(form, parentId, parentName);
        Result success = Result.SUCCESS();
        return success;
    }

    /**
     * 查询列表
     *
     * @return
     */
    @Override
    public Result list() {
        List<MenuVo> list = menuMapper.selectAll();
        List<MenuVo> one = new ArrayList<>();
        List<MenuVo> two = new ArrayList<>();
        List<MenuVo> three = new ArrayList<>();

        if (!CollectionUtils.isEmpty(list)) {
            for (MenuVo vo : list) {
                if (StringUtils.isBlank(vo.getParentId())) {
                    one.add(vo);
                } else if (StringUtils.equalsIgnoreCase("1", vo.getType())) {
                    two.add(vo);
                } else {
                    three.add(vo);
                }
                if (StringUtils.isNotBlank(vo.getParentName())) {
                    vo.setParentNames(Arrays.asList(vo.getParentName().split(",")));
                }
            }
        }

        if (!CollectionUtils.isEmpty(two)) {
            for (MenuVo vo : two) {
                List<MenuVo> children = new ArrayList<>();
                if (!CollectionUtils.isEmpty(three)) {
                    for (MenuVo s : three) {
                        if (StringUtils.equalsIgnoreCase(s.getParentId(), vo.getId())) {
                            children.add(s);
                        }
                    }
                }
                vo.setChildren(children);
            }

        }

        if (!CollectionUtils.isEmpty(one)) {
            for (MenuVo vo : one) {
                List<MenuVo> children = new ArrayList<>();
                for (MenuVo s : two) {
                    if (StringUtils.equalsIgnoreCase(s.getParentId(), vo.getId())) {
                        children.add(s);
                    }
                }
                vo.setChildren(children);
            }

        }
        Result success = Result.SUCCESS(one);
        return success;
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Result delete(String id) {
        MenuVo vo = menuMapper.getMenuById(id);
        List<String> ids = new ArrayList<>();
        if (StringUtils.isBlank(vo.getParentId())) {
            //查询下面的二菜单
            List<String> list = menuMapper.selectMenuByParentId(vo.getId());
            //查询三级菜单
            List<String> nodes = menuMapper.selectMenuByParentIds(list);
            ids.addAll(list);
            ids.addAll(nodes);
            ids.add(id);
        } else if (StringUtils.equalsIgnoreCase("1", vo.getType())) {
            //查询三级菜单
            List<String> list = new ArrayList<>();
            list.add(id);
            List<String> nodes = menuMapper.selectMenuByParentIds(list);
            ids.addAll(nodes);
            ids.add(id);
        } else {
            //删除按钮
            ids.add(id);
        }
        //删除菜单
        menuMapper.deleteByIds(ids);
        //删除角色菜单中间表
        menuMapper.deleteRoleMenuByMenuIds(ids);
        return Result.SUCCESS();
    }

    /**
     * 获取上级菜单
     *
     * @return
     */
    @Override
    public Result getParentMenu() {
        List<MenuVo> list = menuMapper.selectParentMenu();
        List<MenuVo> one = new ArrayList<>();
        List<MenuVo> two = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (MenuVo vo : list) {
                if (StringUtils.isBlank(vo.getParentId())) {
                    one.add(vo);
                } else {
                    two.add(vo);
                }
            }
        }

        List<OptionVo> result = new ArrayList<>();

        if (!CollectionUtils.isEmpty(one)) {
            for (MenuVo vo : one) {
                OptionVo top = new OptionVo();
                top.setValue(vo.getId());
                top.setLabel(vo.getName());
                List<OptionVo> nodes = new ArrayList<>();
                if (!CollectionUtils.isEmpty(two)) {
                    for (MenuVo node : two) {
                        if (StringUtils.equalsIgnoreCase(node.getParentId(), vo.getId())) {
                            OptionVo optionVo = new OptionVo();
                            optionVo.setValue(node.getId());
                            optionVo.setLabel(node.getName());
                            nodes.add(optionVo);
                        }
                    }
                }
                if (CollectionUtils.isEmpty(nodes)) {
                    top.setChildren(null);
                } else {
                    top.setChildren(nodes);
                }
                result.add(top);
            }

        }

        return Result.SUCCESS(result);
    }

    @Override
    public Result edit(MenuForm form) {
        String parentName = null;
        String parentId = null;
        if (!CollectionUtils.isEmpty(form.getParentName())) {
            parentId = form.getParentName().get(form.getParentName().size() - 1);
            form.setParentId(parentId);
            parentName = StringUtils.join(form.getParentName(), ",");
        }
        menuMapper.editMenu(form, parentName);
        Result success = Result.SUCCESS();
        return success;
    }
}
